﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GOA_Client
{
    public partial class loginForm : Form
    {
        //Fields
        public ClientGUI clientGUI { get; set; }

        /*
         * Constructor for the loginForm class. Sets up the GUI.
         * @Param clientGUI - required to be able to send commands back to the main GUI.
         */
        public loginForm(ClientGUI clientGUI)
        {
            this.clientGUI = clientGUI;
            InitializeComponent();
            this.usernameText.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
            this.passwordText.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
        }

        /*
         * Closes all running threads, and thus stops the program as a whole.
         */
        private void cancelButton_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /*
         * Checks if password and name are in the database, and gives an error if either is wrong.
         * If both are matching, a connection is created between the client and the server.
         */
        private void loginButton_Click(object sender, EventArgs e)
        {
            if (dbCheckbox != null && dbCheckbox.Checked)
            {
                Database db = new Database();
                System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] data = System.Text.Encoding.ASCII.GetBytes(passwordText.Text);
                data = x.ComputeHash(data);
                String md5Hash = System.Text.Encoding.ASCII.GetString(data);

                if (db.tryLogin(usernameText.Text, md5Hash))
                {
                    clientGUI.init(usernameText.Text, db.userIDNumber);
                    this.Dispose();
                }
                else
                    MessageBox.Show("Gebruikersnaam en/of wachtwoord is onjuist. Probeer opnieuw.");
            }
            else
            {
                clientGUI.init("Test", 1);
                this.Dispose();
            }
        }

        /*
         * Checks the keys that are pressed in the program. If the enter (char 13) is pressed,
         * the loginButton_click method is called upon.
         * If the escape (char 27) is pressed, the cancelButton_click method is called upon.
         */
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                loginButton_Click(null, null);
            }
            else if(e.KeyChar == (char)27)
            {
                cancelButton_Click(null, null);
            }
        }
    }
}
